package com.halflife.parameter;

import com.halflife.comeElectricity.basic.datadic.DataDic;
import com.halflife.comeElectricity.basic.datadic.DataDicRepository;
import com.halflife.comeElectricity.basic.datadic.DataDicType;
import com.halflife.comeElectricity.basic.datadic.DataDicTypeRepository;
import com.halflife.framework.converter.MapHandler;
import com.halflife.framework.domain.Page;
import com.halflife.framework.participant.user.UserView;
import com.halflife.framework.participant.user.UserViewRepository;
import com.halflife.framework.utils.ImageUploadUtil;
import com.halflife.parameter.SearchKeyWord;
import com.halflife.parameter.SearchKeyWordRepository;
import com.halflife.parameter.template.Template;
import com.halflife.parameter.template.TemplateRepository;
import com.halflife.shiro.utils.ShiroUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/")
public class ReportController {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private TemplateRepository templateRepository;
    @Resource
    private DataDicRepository dataDicRepository;
    @Resource
    private DataDicTypeRepository dataDicTypeRepository;
    @Resource
    private SearchKeyWordRepository searchKeyWordRepository;
    @Resource
    private UserViewRepository userViewRepository;

    @RequestMapping("report/list")
    public String index(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                        HttpServletRequest request, Model model) {
        //Top viewed parameters
        if (queryParam == null) {
            queryParam = new HashMap<>();
        }
        queryParam.put("currentPage_", 0);
        queryParam.put("pageSize_", 5);
        List<Template> list = templateRepository.selectListByPage(queryParam);
        model.addAttribute("list",list);
        //Top category
        StringBuilder categoryReportSql = new StringBuilder();
        categoryReportSql.append("select t.productCategory,count(t.id) as viewCount");
        categoryReportSql.append(" from  t_parameter_templates t ");
        categoryReportSql.append(" where t.valid = true ");
        categoryReportSql.append(" group by t.productCategory ");
        List<Template> categoryReportResult = templateRepository.selectMany(categoryReportSql.toString());
        model.addAttribute("categoryReportResult",categoryReportResult);
        //Background Usage
        StringBuilder bgReportSql = new StringBuilder();
        bgReportSql.append("select t.photoType,count(t.id) as viewCount");
        bgReportSql.append(" from  t_parameter_templates t ");
        bgReportSql.append(" where t.valid = true ");
        bgReportSql.append(" group by t.photoType ");
        List<Template> bgReportResult = templateRepository.selectMany(bgReportSql.toString());
        model.addAttribute("bgReportResult",bgReportResult);
        //Top search Keywords
        queryParam.clear();
        queryParam.put("orderSql", "t.ordinal desc");
        queryParam.put("currentPage_", 0);
        queryParam.put("pageSize_", 15);
        List<SearchKeyWord> searchKeyWordList = searchKeyWordRepository.selectListByPage(queryParam);
        model.addAttribute("searchKeyWordList",searchKeyWordList);
        //Top User
        queryParam.clear();
        queryParam.put("orderSql", "t.loginCount desc");
        queryParam.put("currentPage_", 0);
        queryParam.put("pageSize_", 5);
        List<UserView> userViewList = userViewRepository.selectListByPage(queryParam);
        model.addAttribute("userViewList",userViewList);
        return "report/report";
    }
}
